/*
 * @Author: silence
 * @Date: 2022-08-08 13:48:51
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-24 14:29:26
 * @Description: 权限相关
 */

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../main.dart';
import '../../pages/Mine/Language/LanguageController.dart';

class PermissionUtils {
  /**
   * 相机权限
   */
  static Future<bool> permission_camera() async {
    PermissionStatus status = await Permission.camera.request();
    if (status == PermissionStatus.granted) {
      return true;
    }

    LanguageController languageController = LanguageController();
    bool isPromptEnglish = languageController.isPromptEnglish();
    tipsView(isPromptEnglish
        ? "Camera permissions are turned off, please turn them on in your phone Settings"
        : "相机权限已关闭,请在手机设置中打开");
    return false;
  }

  /**
   *  相册权限
   */
  static Future<bool> permission_photo() async {
    PermissionStatus storageStatus = await Permission.storage.request();
    if (storageStatus == PermissionStatus.granted) {
      return true;
    }

    LanguageController languageController = LanguageController();
    bool isPromptEnglish = languageController.isPromptEnglish();
    tipsView(isPromptEnglish
        ? "Album permissions have been turned off, please open them in the phone Settings"
        : "相册权限已关闭,请在手机设置中打开");
    return false;
  }

  /**
   * 推送权限
   */
  static Future<bool> permission_notification() async {
    PermissionStatus status = await Permission.notification.request();
    if (status == PermissionStatus.granted) {
      return true;
    }
    // tipsView("通知权限已关闭,请在手机设置中打开");
    return false;
  }

  ///检查读写权限具体情况
  static Future<bool> checkReadWritePermission() async {
    if (Platform.isIOS) {
      //iOS平台不需要
      return true;
    }

    //获取当前的权限
    PermissionStatus status = await Permission.storage.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else if (status.isDenied) {
      tipsView('文件储存权限已关闭，您将不能下载，是否去开启？');
    } else {
      //未授权则发起一次申请
      status = await Permission.storage.request();
    }
    return false;
  }

  ///安装app权限
  static Future<bool> checkInstallAppPermission() async {
    var status = await Permission.requestInstallPackages.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.requestInstallPackages.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        SilToast.show("权限未开启");
        return false;
      }
    }
  }

  /**
   * 弹窗
   */
  static tipsView(String content) {
    BuildContext context = navigatorKey.currentState!.overlay!.context;
    LanguageController languageController = LanguageController();
    bool isPromptEnglish = languageController.isPromptEnglish();
    SilAlertDialog.showAlertDialog(
        context,
        SilAlertDialogData(
            title: isPromptEnglish ? "Warm tip" : "温馨提示",
            content: content,
            cancelButtonTitle: isPromptEnglish ? "cancel" : '取消',
            confrimButtonTitle: isPromptEnglish ? "Go to Settings" : '去设置'),
        confrimFunc: () {
      openAppSettings();
      if (Navigator.canPop(context)) Navigator.pop(context);
    });
  }

  /**
   * 定位权限
   */
  static Future<bool> permission_location() async {
    PermissionStatus status = await Permission.location.request();
    if (status == PermissionStatus.granted) {
      return true;
    }
    LanguageController languageController = LanguageController();
    bool isPromptEnglish = languageController.isPromptEnglish();
    tipsView(isPromptEnglish
        ? "The location permissions have been turned off, please open them in the phone Settings"
        : "定位权限已关闭,请在手机设置中打开");
    return false;
  }
}
